package com.example.score.controller;

import com.example.score.common.Result;
import com.example.score.dto.StudentInfoListDto;
import com.example.score.dto.StudentInfoListVo;
import com.example.score.dto.StudentInfoVo;
import com.example.score.entity.StudentSubmit;
import com.example.score.mapper.StudentQuestionPdfMapper;
import com.example.score.service.StudentQuestionPdfService;
import com.example.score.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/student")
@RequiredArgsConstructor
public class StudentController {

    private static final Logger log = LoggerFactory.getLogger(StudentController.class);
    private final StudentService studentService;
    private final StudentQuestionPdfMapper studentQuestionPdfMapper;
    @Autowired
    private StudentQuestionPdfService studentQuestionPdfService;

    /**
     * 学生登录
     * @param studentNum 准考证号
     * @param studentName 姓名
     */
    @GetMapping("/login")
    public Result<?> login(
            @RequestParam String studentNum,
            @RequestParam String studentName) {
        try {
            Map<String, Object> data = studentService.login(studentNum, studentName);
            return Result.success(data);
        } catch (Exception e) {
            log.error("学生登录失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取学生作业列表
     * @param studentNum 准考证号
     */
    @GetMapping("/homework/list")
    public Result<?> getHomeworkList(@RequestParam String studentNum,
                                     @RequestParam(required = false) Long subjectId,
                                     @RequestParam(required = false) Long gradeId,
                                     @RequestParam(required = false) Integer type,
                                     @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                     @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        try {
            List<Map<String, Object>> data = studentService.getHomeworkList(studentNum, subjectId, gradeId, startTime, endTime, type);
            return Result.success(data);
        } catch (Exception e) {
            log.error("获取作业列表失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取学科
     */
    @GetMapping("/subject/list")
    public Result<?> getSubjectList(@RequestParam Long studentNum) {
        try {
            List<Map<String, Object>> data = studentService.getSutdentSubjectList(studentNum);
            return Result.success(data);
        } catch (Exception e) {
            log.error("获取科目列表失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 学生pdf下载
     * @param studentId
     * @param subjectId
     * @return
     */
    @GetMapping("pdf/download")
    public Result<?> pdfDownload(@RequestParam Long studentId,
                                 @RequestParam Long examDetailId,
                            @RequestParam Long subjectId) {
        String s = studentQuestionPdfService.pdfDownload(studentId, subjectId, examDetailId);
        return Result.success(s);
    }

    /**
     * 获取学生信息
     */
    @GetMapping("/getStudentInfo")
    public Result<StudentInfoVo> getStudentInfo(
            @RequestParam String studentId,
            @RequestParam Long classId,
            @RequestParam Long examId
    ) {
        StudentInfoVo studentInfoVo = studentService.getStudentInfo(studentId,classId,examId);
        return Result.success(studentInfoVo);
    }

    /**
     * 获取学生信息列表
     */
    @PostMapping("/getStudentInfoList")
    public Result<List<StudentInfoListVo>> getStudentInfoList(@RequestBody StudentInfoListDto studentInfoListDto
    ) {
        List<StudentInfoListVo> studentSubmits = studentService.getStudentInfoList(studentInfoListDto);
        return Result.success(studentSubmits);
    }
}
